package com.woniu.guoguo.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.guoguo.common.Result;
import com.woniu.guoguo.controller.component.OrderInfo;
import com.woniu.guoguo.controller.component.PageInfo;
import com.woniu.guoguo.model.Order;
import com.woniu.guoguo.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Richie
 * @since 2020-11-02
 */
@RestController
@Slf4j
public class OrderController {

    @Autowired
    private IOrderService iOrderService;

    @GetMapping("queryOrder")
    public Result queryOrderpage(@RequestParam(defaultValue = "1") int currPage, @RequestParam(defaultValue = "5") int pageSize, @RequestParam(defaultValue = "null") String description, @RequestParam int userId){
        log.info("执行查询订单操作，参数为{},{},{},{}", currPage, pageSize, description, userId);
        if (description.equals("null")){
            description=null;
        }
        try {
            Page<OrderInfo> pageParm = new Page<>();
            pageParm.setCurrent(currPage);
            pageParm.setSize(pageSize);
            IPage<OrderInfo> orderPage = iOrderService.find(pageParm, description, userId);
            PageInfo<OrderInfo> page = new PageInfo<>();
            page.setContent(orderPage.getRecords());
            page.setCurrPage((int) orderPage.getCurrent());
            page.setPageSize((int) orderPage.getSize());
            page.setTotalPage((int) orderPage.getPages());
            page.setTotalCount(orderPage.getTotal());
            return Result.success(page);

        }catch (Exception e) {
            log.warn("执行查询报错",e);
            return Result.fail(e.getMessage());
        }
    }

    @GetMapping("chargeback")
    public Result charge( Order order){
        iOrderService.changeOrder(order);
        iOrderService.changeShop(order);
        iOrderService.changUser(order);
        return Result.success();
    }

}

